import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'CheckBox-CheckBox-复选框',
      home: Scaffold(
        appBar: AppBar(
          title: Text('CheckBox-CheckBox-复选框'),
        ),
        body: DemoPage(),
      ),
    );
  }
}

class DemoPage extends StatefulWidget{
  @override
  DemoPageState createState() => DemoPageState();
}

class DemoPageState extends State{

  var currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Checkbox(
        //激活时颜色
        activeColor: Colors.red,
        //tristate为true，value的值可能为true，false，null
          tristate: false,
        //是否选中
        value: 0 == currentIndex,
        onChanged: (bool? check){
          setState(() {
            // if(check?){
              currentIndex = 0;
            // }
          });
        },),
        Checkbox(
          //激活时颜色
          activeColor: Colors.red,
          //是否选中
          value: 1 == currentIndex,
          onChanged: (bool? check){
            setState(() {
              // if(check){
                currentIndex = 1;
              // }

            });
          },),
      ],
    );
  }
}